package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.Page;
import com.dongdongshop.data.Result;
import com.dongdongshop.em.ConstantEnum;
import com.dongdongshop.pojo.TbItemCat;
import com.dongdongshop.pojo.TbTypeTemplate;
import com.dongdongshop.service.TbItemCatService;
import com.dongdongshop.service.TbTypeTemplateService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@RequestMapping("itemCat")
public class TbItemCatController {

    @Reference
    private TbItemCatService tbItemCatService;

    @Reference
    private TbTypeTemplateService tbTypeTemplateService;

    @RequestMapping("list")
    public String list(Model model){
        List<TbTypeTemplate> type = tbTypeTemplateService.getTypeInfo(null);
        model.addAttribute("type",type);
        return "admin/item_cat";
    }


    @RequestMapping("getItemCatInfo")
    @ResponseBody
    public Result getItemCatInfo(@RequestParam(defaultValue = "1")Integer pageNum,
                                 @RequestParam(defaultValue = "30")Integer pageSize,Long parentId){
        Page<TbItemCat> page = tbItemCatService.getItemCatInfo(pageNum,pageSize,parentId);
        return Result.build(ConstantEnum.SUCCESS).setRows(page.getRows()).setTotal(page.getTotal()).setResult(page.getRows());
    }

    @RequestMapping("insertItemCatInfo")
    @ResponseBody
    public Result insertItemCatInfo(TbItemCat tbItemCat){
        tbItemCatService.insertItemCatInfo(tbItemCat);
        return Result.build(ConstantEnum.SUCCESS);
    }

    @RequestMapping("updateItemCat")
    @ResponseBody
    public Result updateItemCat(Long id){
        TbItemCat itemCat = tbItemCatService.updateItemCat(id);
        return Result.build(ConstantEnum.SUCCESS).setResult(itemCat);
    }

    @RequestMapping("updateItemCatInfo")
    @ResponseBody
    public Result updateItemCatInfo(TbItemCat tbItemCat){
        tbItemCatService.updateItemCatInfo(tbItemCat);
        return Result.build(ConstantEnum.SUCCESS);
    }

    @RequestMapping("deleteInfo")
    @ResponseBody
    public Result deleteInfo(Long[] ids){
        tbItemCatService.deleteInfo(ids);
        return Result.build(ConstantEnum.SUCCESS);
    }

    @RequestMapping("sublevel")
    @ResponseBody
    private Result sublevel(@RequestParam(defaultValue = "0")Long id){
            List<TbItemCat> tbItemCat = tbItemCatService.sublevel(id);
            return Result.build(ConstantEnum.SUCCESS).setResult(tbItemCat);
    }



}
